import { customPathMeta } from '@/router/custom_path_table'

/**
 * 路径元数据
 * path: 路径
 * needLogin: 需要登录才能访问该路径
 * needPermission: 需要用户有权限才能访问该路径（前提是needLogin=true才有效）
 * showTag: 是否允许在主体框架标签栏上显示该路径的标签
 */
export const pathMeta = {
  PORTAL: { path: '/portal' },
  LOGIN: { path: '/portal/login' },
  REGISTER: { path: '/portal/register' },
  RESET_PASSWORD: { path: '/portal/reset_password' },

  LAYOUT: { path: '/layout', needLogin: true, needPermission: true },
  NOT_AUTHORIZED: { path: '/401' },
  NOT_AUTHORIZED_EMBEDDED: { path: '/401/embedded', needLogin: true, needPermission: false },
  NOT_FOUND: { path: '/404' },
  NOT_CONNECTED: { path: '/500' },

  ...customPathMeta
}

/**
 * 需要登录才可以访问的路径列表
 */
export const needLoginPaths = (() => {
  const needLoginPaths = []
  for (const key in pathMeta) {
    if (pathMeta[key].needLogin) {
      needLoginPaths.push(pathMeta[key].path)
    }
  }
  return needLoginPaths
})()

/**
 * 需要登录后并且要求用户有访问权限才可以访问的路径列表
 */
export const needLoginAndPermissionPaths = (() => {
  const needLoginAndPermissionPaths = []
  for (const key in pathMeta) {
    if (pathMeta[key].needLogin && pathMeta[key].needPermission) {
      needLoginAndPermissionPaths.push(pathMeta[key].path)
    }
  }
  return needLoginAndPermissionPaths
})()

/**
 * 允许在主体框架标签栏上显示标签的路径列表
 */
export const showTagPaths = (() => {
  const showTagPaths = []
  for (const key in pathMeta) {
    if (pathMeta[key].showTag) {
      showTagPaths.push(pathMeta[key].path)
    }
  }
  return showTagPaths
})()

/**
 * 路径表
 * 将pathMeta解构形成下面格式的对象，外部可以通过key快速获取path
 * {
 *   PORTAL: '/portal',
 *   LOGIN: '/portal/login,
 *   REGISTER: '/portal/register'
 * }
 */
export default (() => {
  const pathTable = {}
  for (const key in pathMeta) {
    pathTable[key] = pathMeta[key].path
  }
  return pathTable
})()
